package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.api.config.filter.IFilterConfig;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.common.exception.CustException;
import com.heima.common.exception.CustomException;
import com.heima.model.behavior.dtos.LikesBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.common.constants.article.HotArticleConstants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.threadlocal.AppThreadLocalUtils;
import com.heima.model.mess.app.NewBehaviorDTO;
import com.heima.model.user.pojos.ApUser;
import com.mongodb.client.result.DeleteResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class ApLikesBehaviorServiceImpl implements ApLikesBehaviorService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;

    @Autowired
    RabbitTemplate rabbitTemplate;

    /**
     * 点赞或取消点赞
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult like(LikesBehaviorDTO dto) {
        if (dto.getArticleId() == null) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID, "文章id不能为空");
        }
        ApUser user = AppThreadLocalUtils.getUser();
        if (user == null) {
            CustException.cust(AppHttpCodeEnum.NEED_LOGIN);
        }
        if (dto.getOperation().intValue() != 0 && dto.getOperation().intValue() != 1) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID, "点赞方式只能是0或者1");
        }
        //根据当前登录用户id查询行为实体对象
       /* Query refId = Query.query(Criteria.where("refId").is(user.getId()));
        ApBehaviorEntry apBehaviorEntry = mongoTemplate.findOne(refId, ApBehaviorEntry.class);*/
        //如果是点赞操作判断是否已经点赞了
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());


        if (dto.getOperation().intValue() == 0) {
            Query query = Query.query(Criteria.where("entryId").is(apBehaviorEntry.getId()).and("articleId").is(dto.getArticleId()));
            ApLikesBehavior one = mongoTemplate.findOne(query, ApLikesBehavior.class);
            if (one != null) {
                CustException.cust(AppHttpCodeEnum.NEED_LOGIN, "已经点过赞,无法再点赞");
            }
            ApLikesBehavior apLikesBehavior = new ApLikesBehavior();
            apLikesBehavior.setEntryId(apBehaviorEntry.getId());
            apLikesBehavior.setArticleId(dto.getArticleId());
            apLikesBehavior.setType(dto.getType());
            apLikesBehavior.setCreatedTime(new Date());
            mongoTemplate.save(apLikesBehavior);
        }
        if (dto.getOperation().intValue() == 1) {
            Query query = Query.query(Criteria.where("entryId").is(apBehaviorEntry.getId()).and("articleId").is(dto.getArticleId()));
            ApLikesBehavior one = mongoTemplate.findOne(query, ApLikesBehavior.class);
            if (one == null) {
                CustException.cust(AppHttpCodeEnum.NEED_LOGIN, "请先点赞");
            }
            mongoTemplate.remove(query, ApLikesBehavior.class);
        }
        //发送新增行为消息,用于热带你文章实时计算
        NewBehaviorDTO behaviorDTO = new NewBehaviorDTO();
        behaviorDTO.setType(NewBehaviorDTO.BehaviorType.LIKES);
        behaviorDTO.setArticleId(dto.getArticleId());
        behaviorDTO.setAdd(dto.getOperation().intValue() == 0 ? 1 : -1);
        rabbitTemplate.convertAndSend(HotArticleConstants.HOT_ARTICLE_SCORE_BEHAVIOR_QUEUE,
                JSON.toJSONString(behaviorDTO)
                );
        log.info("成功发送点赞行为消息 ==> 当前文章id:{} ==> 消息内容",dto.getArticleId(),JSON.toJSONString(behaviorDTO));

        return ResponseResult.okResult();
    }
}
